package org.vaadin.vaadinjit.widgetset.client.ui;

import com.google.gwt.core.client.JavaScriptObject;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;

public class VJitBarChart extends VJitChartWidget<JitBarChartOverlay> {
		
	/**
	 * The constructor should first call super() to initialize the component and
	 * then handle any initialization relevant to Vaadin.
	 */
	public VJitBarChart() {
		super();
	}
	
	
	
	@Override
	protected void initialize(UIDL initUidl) {
		super.initialize(initUidl);
	}

	/**
	 * Called whenever an update is received from the server 
	 */
	@Override
	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
	}
	
	@Override
	protected native JitBarChartOverlay initJit(JavaScriptObject jitConfig) /*-{
		var elName = jitConfig.injectInto;
		var tipsJson = {  
			enable: true,  
			onShow: function(tip, elem) {  
				$wnd.$jit.fireChartHoverLabel(elName, elem.name);
				tip.innerHTML = "<b>" + elem.name + "</b>: " + elem.value;
			}  
		};
		jitConfig['Tips'] = tipsJson;
		return new $wnd.$jit.BarChart(jitConfig);
	}-*/;
	
	@Override
	protected void setupConfig(JitConfiguration config) {
		super.setupConfig(config);
		config.setOption("barsOffset", 20);
		config.setOption("orientation", "vertical");
		config.setOption("labelOffset", 5);
		config.setOption("showAggregates", true);
		config.setOption("showLabels", true);
		
		JitConfiguration label = new JitConfiguration();
		label.setOption("type", getLabelType());
		label.setOption("size", 13);
		label.setOption("family", "Arial");
		label.setOption("color", getTextColor());
		
		config.setOption("Label", label);

	}
	
	@Override
	protected native void handleNativeResize(JitBarChartOverlay overlay) /*-{
	
		var orient = overlay.config.orientation;
	
	    var size = overlay.st.canvas.getSize(),
	        margin = overlay.config.Margin;
	    if(orient == 'horizontal') {
	      	overlay.st.config.offsetX = size.width/2 - margin.left
	        	- (overlay.config.showLabels && (overlay.config.labelOffset + overlay.config.Label.size));    
	      	overlay.st.config.offsetY = (margin.bottom - margin.top)/2;
	    } else {
	      	overlay.st.config.offsetY = -size.height/2 + margin.bottom 
	        	+ (overlay.config.showLabels && (overlay.config.labelOffset + overlay.config.Label.size));
	      	overlay.st.config.offsetX = (margin.right - margin.left)/2;
	    }
	    
	   	overlay.st.refresh();
    	overlay.normalizeDims();
    	overlay.st.plot();
    	
    	if (orient == 'horizontal') {
	    	overlay.st.fx.animate({
	      		modes: ['node-property:width:dimArray'],
	      		duration:1500,
	      		onComplete: function() {
	        		that.busy = false;
	        		callback && callback.onComplete();
	      		}
	      	});
	    }
	    else {
	      	overlay.st.fx.animate({
	      		modes: ['node-property:height:dimArray'],
	      		duration:1500
	      	});
	    }
	}-*/;

}
